﻿using PSDLib.Download;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings
{
    public partial class FrmDownloadServer : Form
    {
        public dl_server_config ServerConfig { get; set; }
        public FrmDownloadServer()
        {
            InitializeComponent();

            cmbStream.Items.Add("主码流");
            cmbStream.Items.Add("副码流");
            cmbStream.SelectedIndex = 0;

            var dsType = Enum.GetNames(typeof(DownloadServerType));
            foreach (var t in dsType)
            {
                cmbServerType.Items.Add(t);
            }

            cmbServerType.SelectedIndex = 0;
        }

        public void SetServerConfig(dl_server_config server)
        {
            if (server != null)
            {
                ServerConfig = server;
                this.txtServerName.Text = server.storage_server_name;
                this.txtServerIP.Text = server.storage_server_ip;
                this.txtServerPort.Text = server.storage_server_port.ToString();
                this.txtServerUser.Text = server.storage_server_user;
                this.txtServerPassword.Text = server.storage_server_password;
                this.cmbStream.SelectedIndex = server.stream_type;
                this.cmbServerType.SelectedItem = server.storage_server_type;
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (ServerConfig == null)
            {
                ServerConfig = new dl_server_config();
                ServerConfig.id = Guid.NewGuid().ToString();
            }

            ServerConfig.storage_server_name = this.txtServerName.Text.Trim();
            ServerConfig.storage_server_ip = this.txtServerIP.Text.Trim();
            ServerConfig.storage_server_port = Convert.ToInt32(this.txtServerPort.Text.Trim());
            ServerConfig.storage_server_user = this.txtServerUser.Text.Trim();
            ServerConfig.storage_server_password = this.txtServerPassword.Text.Trim();
            ServerConfig.stream_type = this.cmbStream.SelectedIndex;
            ServerConfig.storage_server_type = this.cmbServerType.SelectedItem.ToString();
            ServerConfig.file_type = (ServerConfig.storage_server_type == DownloadServerType.DaHua.ToString() ? ".dav" : ".mp4");

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ServerConfig = null;
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            IDownloadServer ds = DownloadServerFactory.Create(cmbServerType.SelectedItem.ToString());
            ds.ServerIP = this.txtServerIP.Text.Trim();
            ds.ServerPort = Convert.ToInt32(this.txtServerPort.Text.Trim());
            ds.ServerUser = this.txtServerUser.Text.Trim();
            ds.ServerPassword = this.txtServerPassword.Text.Trim();

            bool success = false;
            success = ds.Init();

            if (!success)
            {
                MessageBox.Show("初始化失败，请检查接口文件是否完整");
                return;
            }

            success = ds.Login();
            if (success)
            {
                MessageBox.Show("连接成功！");
            }
            else
            {
                MessageBox.Show("连接失败,错误码：" + ds.GetLastError());
            }

            ds.Logout();
            ds.Cleanup();
        }
    }
}
